import 'package:ecy_app_flutter/routes/goods/model/EcyGoodsHomeCategoryListModel.dart';
import 'package:ecy_app_flutter/routes/goods/model/EcyGoodsLabelListModel.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../model/EcyGoodsHomeCategoryModel.dart';

class EcyGoodsCategoryListView extends StatefulWidget {
  final int currentIndex;
  final EcyGoodsHomeCategoryListModel listModel;
  late _EcyGoodsCategoryListViewState currentState;
  final ValueChanged<EcyGoodsHomeCategoryModel> onSelectNewValue;

  @override
  State<StatefulWidget> createState() {
    currentState = _EcyGoodsCategoryListViewState(
        listModel, currentIndex, onSelectNewValue);
    return currentState;
  }

  EcyGoodsCategoryListView(
      this.listModel, this.currentIndex, this.onSelectNewValue);

  reloadData(EcyGoodsHomeCategoryListModel newListModel) {
    currentState.reloadData(newListModel);
  }

  reloadIndexData(int index) {
    currentState.reloadIndexData(index);
  }
}

class _EcyGoodsCategoryListViewState extends State {
  late int currentIndex;
  late EcyGoodsHomeCategoryListModel listModel;
  final ValueChanged<EcyGoodsHomeCategoryModel> onSelectNewValue;
  _EcyGoodsCategoryListViewState(
      this.listModel, this.currentIndex, this.onSelectNewValue);

  @override
  void initState() {
    super.initState();
  }

  reloadData(EcyGoodsHomeCategoryListModel newListModel) {
    if(mounted) {
      setState(() {
        listModel = newListModel;
      });
    }
  }

  reloadIndexData(int index) {
    if(mounted) {
      setState(() {
        currentIndex = index;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
        shrinkWrap: true,
        padding: EdgeInsets.only(left: 14.w, right: 14.w),
        physics: const AlwaysScrollableScrollPhysics(),
        scrollDirection: Axis.horizontal, // 设置滚动方向为水平
        itemCount: listModel.records.length,
        itemBuilder: (context, index) {
          var item = listModel.records[index];
          return GestureDetector(
            child: Container(
                height: 32.w,
                margin: EdgeInsets.only(right: 8.w),
                alignment: Alignment.center,
                child: Container(
                    width: 64.w,
                    height: 32.w,
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.all(Radius.circular(32.r)),
                      color: currentIndex == index
                          ? const Color(0xFFEA4B9B)
                          : const Color(0xFFF5F5F5),
                    ),
                    child: Center(
                        child: Text(item.name ?? item.remark ?? "",
                            textAlign: TextAlign.center,
                            style: TextStyle(
                                fontWeight: currentIndex == index
                                    ? FontWeight.w900
                                    : FontWeight.w500,
                                fontSize: 14.sp,
                                color: currentIndex == index
                                    ? Color(0xFFFFFFFF)
                                    : Color(0xFF8C8C8C)))))),
            onTap: () {
              if (onSelectNewValue != null) {
                item.index = index;
                onSelectNewValue(item);
                setState(() {
                  currentIndex = index;
                });
              }
            },
          );
        });
  }

  @override
  void dispose() {
    // 清理控制器，避免内存泄漏
    super.dispose();
  }
}
